<?php

class ProductsAction extends CommonAction
{

    public function _initialize() {
        parent::_initialize();
        C('LAYOUT_NAME', 'Layout/columns-left');
    }

    public function index() {

        $year = $this->_get('year');
        if($year != 0) {
            $selected['year'] = $year;
        }
        $this->assign('year',$year);

        $mate = $this->_get('mate');
        if($mate != 0) {
            $selected['mate'] = $mate;
        }
        $this->assign('mate',$mate);

        $style = $this->_get('style');
        if($style != 0) {
            $selected['style'] = $style;
        }
        $this->assign('style',$style);
        $this->assign('selected', $selected);
        $map = array();
        import("@.ORG.Util.Page");       //导入分页类
        $model = M('Product');
        $keyword = $this->_get('keyword');
        $map['identify'] = '';
        if ($keyword) {
            $where['name'] = array('like', '%' . $keyword . '%');
            $where['_logic'] = 'or';
            $map['_complex'] = $where;
            $this->assign('keyword', urldecode($keyword));
        }

        $count = $model->where($map)->count();    //计算总数

        $p = new Page($count, 6);
        $rows = $model
                ->where($map)
                ->limit($p->firstRow . ',' . $p->listRows)
                ->order('product_id desc')
                ->select();

        $p->SetPager('zjj','<div class="newpager">總記錄數：[&nbsp;{recordcount}&nbsp;]&nbsp;&nbsp;&nbsp;每頁10條&nbsp;&nbsp;&nbsp;当前[&nbsp;{pageindex}&nbsp;] / [&nbsp;{pagecount}&nbsp;]&nbsp;&nbsp;&nbsp;&nbsp;{prev}{list}{next}</div>',array("listlong"=>"6","first"=>"首页","last"=>"尾页","prev"=>"上一页","next"=>"下一页","list"=>"*","jump"=>"select"));        
        $page = $p->show('zjj');            //分页的导航条的输出变量
        $this->assign("page", $page);
        $this->assign("rows", $rows); //数据循环变量
        $this->assign('attritubes', $this->getAttritubes());
        $this->display('index');
    }

    public function getAttritubes(){
        $group = M('attritube_group');
        $groups = $group->order('id asc')->select();
        $attritube = M('Attritube');
        foreach ($groups as $key => $value) {
            $attr = $attritube
            ->field('Attritube.attritube_id,attritube_name,total,group_id')
            ->where("group_id = " . $value['id'])
            ->join('(SELECT count(1) AS total,attritube_id  from attritube_product group by attritube_id ) as A on A.attritube_id = Attritube.attritube_id')
            ->select();
            $groups[$key]['attritubes'] = $attr;
        }

        return $groups;
    }

}